<?php

/**
 * ClassName:发布评论处理器
 * @auditor LeiJunXiang
 * @time 2024/11/19
 */

namespace app\tool\inviteList;

use app\factory\Factory;
use app\model\invite\Comment;
use app\modelOpreate\invite\CommentOpe;
use app\tool\ToolBase;

class SendCommentHandler extends ToolBase
{
    const TOOL_SEND_COMMENT_HANDLER = "SendCommentHandler";
    //父评论对象key/帖子对象key
    protected $commentObjKey;
    //评论内容
    protected $commentContent;
    //评论用户id
    protected $commentUserId;


    /**
     * (true/false)开始评论(父评论对象key/帖子对象key,评论内容,评论用户id)
     */
    public function startComment($commentObjKey, $commentContent, $commentUserId)
    {
        if($commentObjKey&&$commentContent&&$commentUserId) {
            //初始化数据
            $this->initData($commentObjKey, $commentContent, $commentUserId);
            //更新父评论的属性
            $this->updateFatherParam();
            //保存评论
            $this->saveComment();
            //清除数据
            $this->cleanData();
        }else{
            $this->setErrCode(FAIL);
            $this->setErrMsg("数据错误，请重试");
        }
        return $this->isGoOn();
    }
    /**
     * 初始化数据
     */
    public function initData($commentObjKey, $commentContent, $commentUserId)
    {
        //父评论对象key/帖子对象key
	    $this->commentObjKey = $commentObjKey;
        //评论内容
	    $this->commentContent = $commentContent;
        //评论用户id
	    $this->commentUserId = $commentUserId;
    }
    /**
     * 更新父评论的属性
     */
    public function updateFatherParam()
    {
        if(strpos($this->commentObjKey, 'invite')!==false){
            //包含
            return;
        }
        $tool = Factory::getInstance()->getToolObj(CommentOpe::TOOL_COMMENT_OPE);
        //更新评论是否包含子评论(评论key)
        $flag = $tool->updateIsChildComment($this->commentObjKey);
//        if(!$flag){
//            $this->errMsg = "发布评论失败，请重试！";
//            $this->errCode = FAIL;
//        }

    }
    /**
     * (true/false)保存评论
     */
    public function saveComment()
    {
        if ($this->isGoOn()){
            return;
        }
        $modelObj = Comment::createObj();

        //评论内容
        $commentContent = $this->commentContent;
	    $modelObj->setCommentContent($commentContent);
        //评论用户id
        $commentUserId = $this->commentUserId;
	    $modelObj->setCommentUserId($commentUserId);
        //评论时间
        $commentTime = time();
	    $modelObj->setCommentTime($commentTime);
        //父评论对象key
        $fatherObjKey = $this->commentObjKey;
	    $modelObj->setFatherObjKey($fatherObjKey);
        //点赞
        $like = 0;
	    $modelObj->setLike($like);
        //是否有子评论
        $isChildComment = 0;
	    $modelObj->setIsChildComment($isChildComment);
        //是否发布
        $isPublish = 1;
	    $modelObj->setIsPublish($isPublish);
        //是否删除
        $isDelete = 0;
	    $modelObj->setIsDelete($isDelete);

        //发布评论
        $flag = $modelObj->publishComment();
        if(!$flag){
            $this->errMsg = "发布评论失败，请重试！(2)";
            $this->errCode = FAIL;
        }
    }
    /**
     * 清除数据
     */
    public function cleanData()
    {
        $this->commentObjKey = "";
        $this->commentContent = null;
        $this->commentUserId = "";
    }

}